package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-08-20
 * Time: 16:04
 */
class Counter {
    public int count = 0;
    public Object locker = new Object();
    public Integer a;

    synchronized public void increase() {
        count++;
    }

    //锁里面的对象和线程要访问的对象可以不是一个对象
    //锁里面的对象可以是任意一个
    public void increase2() {
        synchronized (locker) {
            count ++;
        }
    }

    public void increase3() {
        synchronized (locker) {
            count ++;
        }
    }
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase2();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase3();
            }
        });

        t1.start();
        //t1.join();

        t2.start();
        //t2.join();

        t1.join();
        t2.join();

        System.out.println("count:" + counter.count);
    }
}
